// 时间戳转换
const formatNumber = (n) => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

const formatTime = (number, format) => {
	if (number) {
		number = parseInt(number) * 1000

		let date = new Date(parseInt(number))
		let arr = []
		let formatStr = ['Y', 'M', 'D', 'h', 'm', 's']
		arr.push(date.getFullYear())
		arr.push(formatNumber(date.getMonth() + 1))
		arr.push(formatNumber(date.getDate()))

		arr.push(formatNumber(date.getHours()))
		arr.push(formatNumber(date.getMinutes()))
		arr.push(formatNumber(date.getSeconds()))

		for (let index in arr) {
			format = format.replace(formatStr[index], arr[index])
		}
		return format
	} else {
		return ''
	}
}

class HBValidate {
	constructor(arg) {}
	/**
	 * 只能输入数字[0-9]
	 */
	isDigits(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		var result = str.match(/^\d+$/);
		if (result == null) return false;
		return true;
	}

	/**
	 * 匹配phone
	 */
	isPhone(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		if(str.length !== 11) return false
		return true;
	}

	/**
	 * 匹配mobile
	 */
	isMobile(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		var result = str.match(/^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(15\d{9})|(18\d{9}))$/);
		if (result == null) return false;
		return true;
	}

	/**
	 * 联系电话(手机/电话皆可)验证   
	 */
	isTel(text) {
		text = text.toString()
		if (isMobile(text) || isPhone(text)) return true;
		return false;
	}
	/**
	 * 匹配密码，长度在6-12之间，只能包含字符、数字和下划线。
	 */
	isPwd(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		return (/^\w{6,12}$/).test(str);
	}

	/**
	 * 判断是否为合法字符(a-zA-Z0-9-_)
	 */
	isRightfulString(str) {
		if (str == null || str == "") return false;
		var result = str.match(/^[A-Za-z0-9_-]+$/);
		if (result == null) return false;
		return true;
	}
	/**
	 * 匹配身份证号码
	 */
	isIdCardNo(num) {
		if (num == null || num == "") {
			uni.showToast({
				title: '身份证号码不能为空',
				icon: 'none'
			})
			return false;
		}

		num = num.toString()

		var len = num.length,
			re;
		if (len == 15)
			re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
		else if (len == 18)
			re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
		else {
			uni.showToast({
				title: '身份证号码位数不对',
				icon: 'none'
			})
			return false;
		}

		var a = num.match(re);
		if (a != null) {
			if (len == 15) {
				var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
				var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
			} else {
				var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
				var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
			}
			if (!B) {
				uni.showToast({
					title: "输入的身份证号 " + a[0] + " 里出生日期不对。",
					icon: 'none'
				})
				return false;
			}
		}
		if (!re.test(num)) {
			uni.showToast({
				title: '身份证最后一位只能是数字和字母',
				icon: 'none'
			})
			return false;
		}

		return true;
	}
	/**
	 * 匹配文件密码，六位数字
	 */
	isFilePwd(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		return (/^\d{6}$/).test(str);
	}
	/**
	 * 匹配银行卡号，13到19位数字
	 */
	isBankNum(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		return (/^\d{13,19}$/).test(str);
	}
	/**
	 * 匹配社会统一信用代码
	 */
	isUSCCode(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		return (/^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g).test(str)
	}
	/**
	 * 匹配价格，也就是钱，money、money、money、money
	 */
	isMoney(str) {
		if (str == null || str == "") return false;
		str = str.toString()
		return (/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/).test(str)
	}
	/**
	 * 匹配车牌号
	 */
	isVehicleNumber(str) {
		if (str == null || str == "") return false;
		str = str.toString().toLocaleUpperCase()
		var xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
		var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
		if (str.length == 7) {
			return creg.test(str);
		} else if (str.length == 8) {
			return xreg.test(str);
		} else {
			return false;
		}
	}
}
// 防止重复点击
class AvoidDoubleClick {
	constructor(args) {
		this.isWorking = false
	}
	// 判断是否正在“工作”，不是则变为正在工作
	isWork() {
		if (this.isWorking) {
			return true
		} else {
			this.isWorking = true
			return false
		}
	}
	toRest() {
		setTimeout(() => {
			this.isWorking = false
		}, 500)
	}
}

class ValidateReport {
	constructor(arg) {
		// 是否需要弹窗 默认需要弹窗
		this.isNeedToast = arg ? false : true
	}
	/**
	 * 验证不为空
	 */
	vdItemNotNone(str, word) {
		if (str == null || str == "" || str == undefined) {
			if (this.isNeedToast) {
				uni.showToast({
					title: word + '不能为空',
					duration: 2000,
					icon: 'none',
				})
			}
			return false;
		} else {
			return true
		}
	}
	/**
	 * 食材品质描述
	 */
	vdFoodQuality(str) {
		if (str == null || str == "" || str == undefined) {
			if (this.isNeedToast) {
				uni.showToast({
					title: '食材品质描述不能为空',
					duration: 2000,
					icon: 'none',
				})
			}
			return false;
		} else {
			return true
		}
	}
	/**
	 * 食材价格
	 */
	vdFoodPirce(str) {
		const validate = new HBValidate()
		if (str == null || str == "" || str == undefined) {
			if (this.isNeedToast) {
				uni.showToast({
					title: '采购单价格不能为空',
					duration: 2000,
					icon: 'none',
				})
			}
			return false;
		} else if (!validate.isMoney(str)) {
			if (this.isNeedToast) {
				uni.showToast({
					title: '采购单价格式错误',
					duration: 2000,
					icon: 'none',
				})
			}
			return false;
		} else {
			return true
		}
	}
}

// 预览图片
const previewImage = (imgPath, curr) => {
	uni.previewImage({
		urls: imgPath,
		current : curr,
		longPressActions: {
			itemList: ['发送给朋友', '保存图片', '收藏'],
			success: data => {
				console.log(data)
			},
			fail: err => {
				console.log(err)
			}
		}
	});
}

module.exports = {
	formatTime,
	HBValidate,
	AvoidDoubleClick,
	ValidateReport,
	previewImage,
}
